package test;

import java.util.Scanner;

public class jiaqi {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
        int t = scan.nextInt();
        long[][] a=new long[t][4];
        for(int i=0;i<t;i++){
            for(int j=0;j<4;j++){
                a[i][j]=scan.nextLong();
            }
        }
        for(int i=0;i<t;i++){
            long l = 1;
            long r = Long.MAX_VALUE;
            long count = 0;
            long ans=0;
            long k = a[i][3];
            while (l <=r) {
                long m = (l + r) / 2;
                count = shuliang(a[i][0], a[i][1], a[i][2], m);
                if (count >=k) {
                    r = m -1;
                    ans = m;
                } else {
                    l = m + 1;
                }
            }
            System.out.println(ans);

        }
        scan.close();
    }
    static long max_gys(long a,long b){
         if(b>0){
             return max_gys(b,a%b);
         }else{
             return a;
         }
    }
    static long min_gbs(long a,long b){
        return a/max_gys(a,b)*b;
    }


    static long shuliang(long a,long b,long c,long d) {
        long gbs_ab =min_gbs(a,b);
        long gbs_ac =min_gbs(a,c);
        long gbs_bc=min_gbs(b,c);
        long gbs_abc=min_gbs(gbs_ab,c);
        return d/a + d/b + d/c - d/gbs_ab - d/gbs_ac - d/gbs_bc + d/gbs_abc;
    }
}
